<?php
require_once("inc.admin.php");

$this_title="$vars[admin_title] &raquo; ".__("Product Categories");
$page_title=__("Product Categories");

if(!$pv["task"]["Product Categories"]){
	$errmsg="<h2>$page_title</h2>".format_err(__("You do not have the privilege to manage product category."));

	print format_admin_page($errmsg, $this_title);
	exit();
}

$add_edit_file=$vars["file"]["admin"]["add_edit_product_category"];

//#####CATEGORY POST#####
if($_POST["__req"]){
	if(!$post_s["id"] || !is_numeric($post_s["id"]) || $post_s["id"]<=0){
		$errmsg.=__("Invalid category ID provided.")."<br />\n";
	}elseif(!@mysql_num_rows(mysql_query("select * from $db->product_category where id='$post_d[id]'"))){
		$errmsg.=replace_tag(__("Category ID: '<%id%>' does not exist in the system."), array("<%id%>"=>$post_h["id"]))."<br />\n";
	}elseif(!cat_deletable($post_d["id"])){
		$errmsg.=replace_tag(__("Category ID: '<%id%>' cannot be deleted. It has sub-category or has products linked to it."), array("<%id%>"=>$post_h["id"]))."<br />\n";
	}

	if(!$errmsg){
		if(!mysql_query($sql="delete from $db->product_category where id='$post_d[id]'")){
			$errmsg.=__("Error deleting the category.")."<br />\n<br />\n".__("SQL:")." $sql<br />\n<br />\n".__("Error:")." ".mysql_error()."<br />\n";
			$critical_error=$errmsg;
		}else{
			$msg=replace_tag(__("The category ID: '<%id%>' has been successfully deleted."), array("<%id%>"=>$post_h["id"]))."<br />\n";
		}
	}

	//log critical error
	if($critical_error){
		$error_type_code="a-cd";
		$affected_uid=0;
		log_activity('e', 'a', $aid, 'n', $affected_uid, $error_type_code, $critical_error);
	}

	$msg=$msg? format_msg($msg) : "";
	$errmsg=$errmsg? format_err("There is some error(s):<br />\n<br />\n$errmsg") : "";
}

//javascript
ob_start();
?>
<script type="text/javascript" src="<?php echo JS_URL."/get_file_gzip.php?file=".urlencode("jquery.js"); ?>"></script>
<script type="text/javascript">
jQuery(document).ready(function(j){
	j('a[@name=delete_cat]').click(function(){
		id = j(this).attr('rel');
		confirm_text='<?php echo AddSlashes(__("Delete this category with ID: <#id#>, are you sure?")); ?>';
		confirm_text=confirm_text.replace(/\<#id#\>/g, id);
		if(confirm(confirm_text)){
			j('input[@name=id]').val(id);
			j('form[@name=cat_form]').submit();
		}
		return false;
	});
});
</script>
<?php
$jvscript = ob_get_clean();

//#####LIST CATEGORY#####
$cats=get_all_subcat_by_maincat();

$k=0;
for($i=0,$total_cat=count($cats);$i<$total_cat;$i++){
	$edit_link="<a href=\"$add_edit_file?type=edit&id=".$cats[$i]["id"]."\"><img src='".M_URL."/images/icons/edit.png'></a>";
	$del_link="<a href='#' name='delete_cat' rel='".$cats[$i]["id"]."'><img src='".M_URL."/images/icons/delete.png'></a>";
	$cat_full=cat_full_display($cats[$i]["id"]);
	$cat_row.="
	<tr class='row$k'>
		<td>$edit_link</td>
		<td>$del_link</td>
		<td>".$cats[$i]["id"]."</td>
		<td>".$cats[$i]["title"]."</td>
		<td>$cat_full</td>
	</tr>";
	$k=1-$k;
}

//listing text
$listing_text=replace_tag(__("Listing <%x%> <%category%>."), array("<%x%>"=>strval($total_cat), "<%category%>"=>$total_cat>1? __("categories") : __("category")));

//listing
$cat_list=($errmsg || $msg? $errmsg.$msg : "").
"<input type='button' value='".__("Add Category")."' onclick=\"window.location='$add_edit_file?type=add';\" />
<br />\n
$listing_text
<form name='cat_form' method='post' action='$this_file'>
<input type='hidden' name='__req' value='1' />
<input type='hidden' name='id' />
<table class='aml_table'>
	<tr class='aml_header'>
		<td width='18'><img src='".M_URL."/images/icons/edit.png'></td>
		<td width='18'><img src='".M_URL."/images/icons/delete.png'></td>
		<td>ID</td>
		<td>Title</td>
		<td>Category Path</td>
	</tr>
	$cat_row
</table>
</form>";
//#####END CATEGORY LIST#####

$content="<h2>$page_title</h2>$cat_list";

print format_admin_page($content, $this_title, $jvscript);

function cat_deletable($cat_id){
	global $vars;
	$db=$vars["db"];

	$deletable = true;
	//category child
	if(@mysql_num_rows(mysql_query("select * from $db->product_category where parent_id='$cat_id'"))){
		$deletable = false;
		//check for product category link
	}elseif(@mysql_num_rows(mysql_query("select * from $db->products where catid='$cat_id'"))){
		$deletable = false;
	}

	return $deletable;
}
?>